const url = require("url");
const {query} = require('../service/menu.js')

function eventSource(request, response) {
	const parsedURL = url.parse(request.url, true);
	let pathname = parsedURL.pathname;
	if (pathname === "/EventSource") {
		response.writeHead(200, {
			"Content-Type": "text/event-stream",// 规定把报头设置为 text/event-stream
			"Cache-Control": "no-store",// 设置不对页面进行缓存
			"Access-Control-Allow-Origin": "*"
		});
		let offset = 0
		setInterval(() => {
			if (offset > 10) {
				offset = 0
			}else {
				offset += 1
			}
			query(10,offset).then(res => {
				response.write("data:" + JSON.stringify(res) + "\n\n");
			})
		},1000)
		response.on("close", function () {
		});
	}
}

module.exports = {
	eventSource
}
